package gr.unipi.mscd1;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import org.snmp4j.mp.SnmpConstants;

import javax.swing.JTextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.text.DecimalFormat;

import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.Font;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ScrollPaneConstants;

public class MainForm extends JFrame implements TableModelListener, ListSelectionListener {
	
	private static final long serialVersionUID = 1L;
	private JTextField txtAgentAddr;
	private JPanel pnlMain;
	private JButton btnSubmit;
	private JLabel lblAgentAddr;
	private JPanel pnlTop;
	private JComboBox cmbProtocol;
	private JLabel lblSemiColon;
	private JLabel lblSlash;
	private JTextField txtAgentPort;
	
	private SimpleSnmpClient client;
	private JComboBox cmbSnmpVersion;
	private JPanel pnlIfInfo;
	private JPanel pnlGenInfo;
	private JPanel pnlGenInfoData;
	private JLabel lblSysName;
	JTextArea txtSysName;
	private JLabel lblSysDescr;
	JTextArea txtSysDescr;
	private JLabel lblSysLocation;
	JTextArea txtSysLocation;
	private JLabel lblSysContact;
	JTextArea txtSysContact;
	private JLabel lblSysUpTime;
	JTextArea txtSysUpTime;
	private JPanel pnlGenInfoTitle;
	private JLabel lblGenInfoTitle;
	private JSplitPane splitPane;
	private JScrollPane scrollPane;
	private JPanel pnlIfDetails;
	private JTable table;
	private JPanel pnlIfTop;
	private JPanel pnlIfTitle;
	private JLabel lblIfTitle;
	private JPanel pnlIfGeneral;
	private JLabel lblIfCount;
	JTextArea txtIfCount;
	private JLabel lblIfActiveCount;
	private JTextArea txtIfActive;
	private JLabel lblIfInList;
	private JTextArea txtIfInList;
	private JLabel lblIfPerc;
	private JTextArea txtIfPerc;
	private IfInfoDetailsPanel pnlIfDet;
	private JLabel lblTcpSegSum;
	JTextArea txtSegIn;
	private JLabel lblErrsIn;
	JTextArea txtErrsIn;
	private JLabel lblTcpRetrans;
	JTextArea txtTcpRetrans;
	private JLabel lblAlg;
	JTextArea txtAlg;
	private JLabel lblTimeout;
	JTextArea txtTimeoutMax;
	private JLabel lblTxtTimeoutMin;
	JTextArea txtTimeoutMin;
	private JLabel lblTcpEstNo;
	JTextArea txtEstNo;
	private JScrollPane scrollPane1;
	private JButton btnHostResources;
	
	public MainForm() {
		pnlIfDet = null;
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				doDisconnect();
			}
		});
		client = null;
		setTitle("SNMP manager");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		getContentPane().setBackground(Color.WHITE);
		
		pnlTop = new JPanel();
		FlowLayout fl_pnlTop = (FlowLayout) pnlTop.getLayout();
		fl_pnlTop.setAlignment(FlowLayout.LEFT);
		pnlTop.setBackground(Color.WHITE);
		getContentPane().add(pnlTop, BorderLayout.NORTH);
		
		lblAgentAddr = new JLabel("Snmp Agent IP Address:");
		lblAgentAddr.setHorizontalAlignment(SwingConstants.LEFT);
		pnlTop.add(lblAgentAddr);
		
		cmbSnmpVersion = new JComboBox();
		cmbSnmpVersion.setModel(new DefaultComboBoxModel(new String[] {"v1", "v2c", "v3"}));
		cmbSnmpVersion.setSelectedIndex(1);
		pnlTop.add(cmbSnmpVersion);
		
		cmbProtocol = new JComboBox();
		cmbProtocol.setModel(new DefaultComboBoxModel(new String[] {"udp", "tcp"}));
		cmbProtocol.setSelectedIndex(0);
		pnlTop.add(cmbProtocol);
		
		lblSemiColon = new JLabel(":");
		pnlTop.add(lblSemiColon);
		
		txtAgentAddr = new JTextField();
		txtAgentAddr.setText("localhost");
		pnlTop.add(txtAgentAddr);
		txtAgentAddr.setColumns(15);
		
		btnSubmit = new JButton("Connect");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (btnSubmit.getText().equalsIgnoreCase("Connect")) {
					doConnect();
				}
				else {
					doDisconnect();
				}
			}
		});
		
		lblSlash = new JLabel("/");
		pnlTop.add(lblSlash);
		
		txtAgentPort = new JTextField();
		txtAgentPort.setText("161");
		pnlTop.add(txtAgentPort);
		txtAgentPort.setColumns(5);
		pnlTop.add(btnSubmit);
		
		pnlMain = new JPanel();
		pnlMain.setBackground(Color.WHITE);
		getContentPane().add(pnlMain, BorderLayout.CENTER);
		pnlMain.setLayout(new BorderLayout(0, 0));
		
		pnlGenInfo = new JPanel();
		pnlGenInfo.setBackground(Color.WHITE);
		pnlMain.add(pnlGenInfo, BorderLayout.NORTH);
		pnlGenInfo.setLayout(new BorderLayout(0, 0));
		
		pnlGenInfoData = new JPanel();
		pnlGenInfoData.setBackground(Color.WHITE);
		pnlGenInfo.add(pnlGenInfoData, BorderLayout.WEST);
		GridBagLayout gbl_pnlGenInfoData = new GridBagLayout();
		gbl_pnlGenInfoData.columnWidths = new int[] {100, 400, 50, 100, 400};
		gbl_pnlGenInfoData.rowHeights = new int[] {20, 20, 20, 20, 20, 30, 0};
		gbl_pnlGenInfoData.columnWeights = new double[]{1.0, 1.0, 0.0, 1.0, 1.0};
		gbl_pnlGenInfoData.rowWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
		pnlGenInfoData.setLayout(gbl_pnlGenInfoData);
		
		lblSysName = new JLabel("System Name:");
		GridBagConstraints gbc_lblSysName = new GridBagConstraints();
		gbc_lblSysName.anchor = GridBagConstraints.EAST;
		gbc_lblSysName.insets = new Insets(5, 5, 5, 5);
		gbc_lblSysName.gridx = 0;
		gbc_lblSysName.gridy = 0;
		pnlGenInfoData.add(lblSysName, gbc_lblSysName);
		
		txtSysName = new JTextArea();
		txtSysName.setName("txtSysName");
		txtSysName.setEditable(false);
		GridBagConstraints gbc_txtSysName = new GridBagConstraints();
		gbc_txtSysName.fill = GridBagConstraints.BOTH;
		gbc_txtSysName.insets = new Insets(5, 5, 5, 5);
		gbc_txtSysName.gridx = 1;
		gbc_txtSysName.gridy = 0;
		pnlGenInfoData.add(txtSysName, gbc_txtSysName);
		
		lblTcpSegSum = new JLabel("Tcp Segments In:");
		GridBagConstraints gbc_lblTcpSegSum = new GridBagConstraints();
		gbc_lblTcpSegSum.anchor = GridBagConstraints.EAST;
		gbc_lblTcpSegSum.insets = new Insets(5, 5, 5, 5);
		gbc_lblTcpSegSum.gridx = 3;
		gbc_lblTcpSegSum.gridy = 0;
		pnlGenInfoData.add(lblTcpSegSum, gbc_lblTcpSegSum);
		
		txtSegIn = new JTextArea();
		txtSegIn.setEditable(false);
		GridBagConstraints gbc_txtSegIn = new GridBagConstraints();
		gbc_txtSegIn.insets = new Insets(5, 5, 5, 0);
		gbc_txtSegIn.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtSegIn.gridx = 4;
		gbc_txtSegIn.gridy = 0;
		pnlGenInfoData.add(txtSegIn, gbc_txtSegIn);
		
		lblSysDescr = new JLabel("System Description:");
		GridBagConstraints gbc_lblSysDescr = new GridBagConstraints();
		gbc_lblSysDescr.anchor = GridBagConstraints.EAST;
		gbc_lblSysDescr.insets = new Insets(5, 5, 5, 5);
		gbc_lblSysDescr.gridx = 0;
		gbc_lblSysDescr.gridy = 1;
		pnlGenInfoData.add(lblSysDescr, gbc_lblSysDescr);
		
		txtSysDescr = new JTextArea();
		txtSysDescr.setName("txtSysDescr");
		txtSysDescr.setEditable(false);
		GridBagConstraints gbc_txtSysDescr = new GridBagConstraints();
		gbc_txtSysDescr.fill = GridBagConstraints.BOTH;
		gbc_txtSysDescr.insets = new Insets(5, 5, 5, 5);
		gbc_txtSysDescr.gridx = 1;
		gbc_txtSysDescr.gridy = 1;
		pnlGenInfoData.add(txtSysDescr, gbc_txtSysDescr);
		
		lblErrsIn = new JLabel("Tcp Errors:");
		GridBagConstraints gbc_lblErrsIn = new GridBagConstraints();
		gbc_lblErrsIn.anchor = GridBagConstraints.EAST;
		gbc_lblErrsIn.insets = new Insets(5, 5, 5, 5);
		gbc_lblErrsIn.gridx = 3;
		gbc_lblErrsIn.gridy = 1;
		pnlGenInfoData.add(lblErrsIn, gbc_lblErrsIn);
		
		txtErrsIn = new JTextArea();
		txtErrsIn.setEditable(false);
		GridBagConstraints gbc_txtErrsIn = new GridBagConstraints();
		gbc_txtErrsIn.insets = new Insets(5, 5, 5, 0);
		gbc_txtErrsIn.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtErrsIn.gridx = 4;
		gbc_txtErrsIn.gridy = 1;
		pnlGenInfoData.add(txtErrsIn, gbc_txtErrsIn);
		
		lblSysLocation = new JLabel("System Location:");
		GridBagConstraints gbc_lblSysLocation = new GridBagConstraints();
		gbc_lblSysLocation.anchor = GridBagConstraints.EAST;
		gbc_lblSysLocation.insets = new Insets(5, 5, 5, 5);
		gbc_lblSysLocation.gridx = 0;
		gbc_lblSysLocation.gridy = 2;
		pnlGenInfoData.add(lblSysLocation, gbc_lblSysLocation);
		
		txtSysLocation = new JTextArea();
		txtSysLocation.setName("txtSysLocation");
		txtSysLocation.setEditable(false);
		GridBagConstraints gbc_txtSysLocation = new GridBagConstraints();
		gbc_txtSysLocation.fill = GridBagConstraints.BOTH;
		gbc_txtSysLocation.insets = new Insets(5, 5, 5, 5);
		gbc_txtSysLocation.gridx = 1;
		gbc_txtSysLocation.gridy = 2;
		pnlGenInfoData.add(txtSysLocation, gbc_txtSysLocation);
		
		lblTcpRetrans = new JLabel("Tcp Retransmitted:");
		GridBagConstraints gbc_lblTcpRetrans = new GridBagConstraints();
		gbc_lblTcpRetrans.anchor = GridBagConstraints.EAST;
		gbc_lblTcpRetrans.insets = new Insets(5, 5, 5, 5);
		gbc_lblTcpRetrans.gridx = 3;
		gbc_lblTcpRetrans.gridy = 2;
		pnlGenInfoData.add(lblTcpRetrans, gbc_lblTcpRetrans);
		
		txtTcpRetrans = new JTextArea();
		txtTcpRetrans.setEditable(false);
		GridBagConstraints gbc_txtTcpRetrans = new GridBagConstraints();
		gbc_txtTcpRetrans.insets = new Insets(5, 5, 5, 0);
		gbc_txtTcpRetrans.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTcpRetrans.gridx = 4;
		gbc_txtTcpRetrans.gridy = 2;
		pnlGenInfoData.add(txtTcpRetrans, gbc_txtTcpRetrans);
		
		lblSysContact = new JLabel("System Contact:");
		GridBagConstraints gbc_lblSysContact = new GridBagConstraints();
		gbc_lblSysContact.anchor = GridBagConstraints.EAST;
		gbc_lblSysContact.insets = new Insets(5, 5, 5, 5);
		gbc_lblSysContact.gridx = 0;
		gbc_lblSysContact.gridy = 3;
		pnlGenInfoData.add(lblSysContact, gbc_lblSysContact);
		
		txtSysContact = new JTextArea();
		txtSysContact.setName("txtSysContact");
		txtSysContact.setEditable(false);
		GridBagConstraints gbc_txtSysContact = new GridBagConstraints();
		gbc_txtSysContact.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtSysContact.anchor = GridBagConstraints.NORTH;
		gbc_txtSysContact.insets = new Insets(5, 5, 5, 5);
		gbc_txtSysContact.gridx = 1;
		gbc_txtSysContact.gridy = 3;
		pnlGenInfoData.add(txtSysContact, gbc_txtSysContact);
		
		lblAlg = new JLabel("Algorithm:");
		GridBagConstraints gbc_lblAlg = new GridBagConstraints();
		gbc_lblAlg.anchor = GridBagConstraints.EAST;
		gbc_lblAlg.insets = new Insets(5, 5, 5, 5);
		gbc_lblAlg.gridx = 3;
		gbc_lblAlg.gridy = 3;
		pnlGenInfoData.add(lblAlg, gbc_lblAlg);
		
		txtAlg = new JTextArea();
		txtAlg.setEditable(false);
		GridBagConstraints gbc_txtAlg = new GridBagConstraints();
		gbc_txtAlg.insets = new Insets(5, 5, 5, 0);
		gbc_txtAlg.fill = GridBagConstraints.BOTH;
		gbc_txtAlg.gridx = 4;
		gbc_txtAlg.gridy = 3;
		pnlGenInfoData.add(txtAlg, gbc_txtAlg);
		
		lblSysUpTime = new JLabel("System Up Time:");
		GridBagConstraints gbc_lblSysUpTime = new GridBagConstraints();
		gbc_lblSysUpTime.anchor = GridBagConstraints.EAST;
		gbc_lblSysUpTime.insets = new Insets(5, 5, 5, 5);
		gbc_lblSysUpTime.gridx = 0;
		gbc_lblSysUpTime.gridy = 4;
		pnlGenInfoData.add(lblSysUpTime, gbc_lblSysUpTime);
		
		txtSysUpTime = new JTextArea();
		txtSysUpTime.setName("txtSysUpTime");
		txtSysUpTime.setEditable(false);
		GridBagConstraints gbc_txtSysUpTime = new GridBagConstraints();
		gbc_txtSysUpTime.fill = GridBagConstraints.BOTH;
		gbc_txtSysUpTime.insets = new Insets(5, 5, 5, 5);
		gbc_txtSysUpTime.gridx = 1;
		gbc_txtSysUpTime.gridy = 4;
		pnlGenInfoData.add(txtSysUpTime, gbc_txtSysUpTime);
		
		lblTimeout = new JLabel("Timeout Max (sec):");
		GridBagConstraints gbc_lblTimeout = new GridBagConstraints();
		gbc_lblTimeout.anchor = GridBagConstraints.EAST;
		gbc_lblTimeout.insets = new Insets(5, 5, 5, 5);
		gbc_lblTimeout.gridx = 3;
		gbc_lblTimeout.gridy = 4;
		pnlGenInfoData.add(lblTimeout, gbc_lblTimeout);
		
		txtTimeoutMax = new JTextArea();
		txtTimeoutMax.setEditable(false);
		GridBagConstraints gbc_txtTimeoutMax = new GridBagConstraints();
		gbc_txtTimeoutMax.insets = new Insets(5, 5, 5, 0);
		gbc_txtTimeoutMax.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTimeoutMax.gridx = 4;
		gbc_txtTimeoutMax.gridy = 4;
		pnlGenInfoData.add(txtTimeoutMax, gbc_txtTimeoutMax);
		
		lblTxtTimeoutMin = new JLabel("Timeout Min (sec):");
		GridBagConstraints gbc_lblTxtTimeoutMin = new GridBagConstraints();
		gbc_lblTxtTimeoutMin.anchor = GridBagConstraints.EAST;
		gbc_lblTxtTimeoutMin.insets = new Insets(5, 5, 5, 5);
		gbc_lblTxtTimeoutMin.gridx = 3;
		gbc_lblTxtTimeoutMin.gridy = 5;
		pnlGenInfoData.add(lblTxtTimeoutMin, gbc_lblTxtTimeoutMin);
		
		txtTimeoutMin = new JTextArea();
		txtTimeoutMin.setEditable(false);
		GridBagConstraints gbc_txtTimeoutMin = new GridBagConstraints();
		gbc_txtTimeoutMin.insets = new Insets(5, 5, 5, 0);
		gbc_txtTimeoutMin.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTimeoutMin.gridx = 4;
		gbc_txtTimeoutMin.gridy = 5;
		pnlGenInfoData.add(txtTimeoutMin, gbc_txtTimeoutMin);
		
		btnHostResources = new JButton("HOST_RESOURCES");
		btnHostResources.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				HostResForm.open(client);
			}
		});
		GridBagConstraints gbc_btnHostResources = new GridBagConstraints();
		gbc_btnHostResources.insets = new Insets(5, 5, 5, 5);
		gbc_btnHostResources.gridx = 0;
		gbc_btnHostResources.gridy = 6;
		pnlGenInfoData.add(btnHostResources, gbc_btnHostResources);
		
		lblTcpEstNo = new JLabel("Tcp established No:");
		GridBagConstraints gbc_lblTcpEstNo = new GridBagConstraints();
		gbc_lblTcpEstNo.anchor = GridBagConstraints.EAST;
		gbc_lblTcpEstNo.insets = new Insets(5, 5, 0, 5);
		gbc_lblTcpEstNo.gridx = 3;
		gbc_lblTcpEstNo.gridy = 6;
		pnlGenInfoData.add(lblTcpEstNo, gbc_lblTcpEstNo);
		
		txtEstNo = new JTextArea();
		txtEstNo.setEditable(false);
		GridBagConstraints gbc_txtEstNo = new GridBagConstraints();
		gbc_txtEstNo.insets = new Insets(5, 5, 0, 0);
		gbc_txtEstNo.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtEstNo.gridx = 4;
		gbc_txtEstNo.gridy = 6;
		pnlGenInfoData.add(txtEstNo, gbc_txtEstNo);
		
		pnlGenInfoTitle = new JPanel();
		pnlGenInfoTitle.setBorder(new LineBorder(new Color(0, 0, 0)));
		pnlGenInfoTitle.setBackground(Color.WHITE);
		pnlGenInfo.add(pnlGenInfoTitle, BorderLayout.NORTH);
		
		lblGenInfoTitle = DefaultComponentFactory.getInstance().createTitle("General Information");
		lblGenInfoTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		pnlGenInfoTitle.add(lblGenInfoTitle);
		
		pnlIfInfo = new JPanel();
		pnlIfInfo.setBackground(Color.WHITE);
		pnlMain.add(pnlIfInfo, BorderLayout.CENTER);
		pnlIfInfo.setLayout(new BorderLayout(0, 0));
		
		pnlIfDetails = new JPanel();
		pnlIfDetails.setBackground(Color.WHITE);
		
		scrollPane1 = new JScrollPane(pnlIfDetails);
		scrollPane1.setBackground(Color.WHITE);
		scrollPane1.getViewport().setBackground(Color.WHITE);
		
		table = new JTable();
		table.setBackground(Color.WHITE);
		table.getSelectionModel().addListSelectionListener(this);
		
		scrollPane = new JScrollPane(table);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setBackground(Color.WHITE);
		scrollPane.getViewport().setBackground(Color.WHITE);
		
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, scrollPane, scrollPane1);
		pnlIfDetails.setLayout(new BorderLayout(0, 0));
		//pnlIfDetails.setLa
		
		pnlIfInfo.add(splitPane, BorderLayout.CENTER);
		
		setBackground(Color.WHITE);
		pnlMain.setVisible(false);
		setBounds(30, 30, 1200, 1000);
		
		setVisible(true);
		
		table.setAutoCreateRowSorter(true);
		
		pnlIfTop = new JPanel();
		pnlIfTop.setBackground(Color.WHITE);
		pnlIfInfo.add(pnlIfTop, BorderLayout.NORTH);
		pnlIfTop.setLayout(new BorderLayout(0, 0));
		
		pnlIfTitle = new JPanel();
		pnlIfTitle.setBorder(new LineBorder(new Color(0, 0, 0)));
		pnlIfTitle.setBackground(Color.WHITE);
		pnlIfTop.add(pnlIfTitle, BorderLayout.NORTH);
		
		lblIfTitle = DefaultComponentFactory.getInstance().createTitle("Interface Information");
		lblIfTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		pnlIfTitle.add(lblIfTitle);
		
		pnlIfGeneral = new JPanel();
		pnlIfGeneral.setBackground(Color.WHITE);
		pnlIfTop.add(pnlIfGeneral, BorderLayout.WEST);
		GridBagLayout gbl_pnlIfGeneral = new GridBagLayout();
		gbl_pnlIfGeneral.columnWidths = new int[] {100, 80, 100, 80, 0};
		gbl_pnlIfGeneral.rowHeights = new int[] {20, 20, 10};
		gbl_pnlIfGeneral.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_pnlIfGeneral.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		pnlIfGeneral.setLayout(gbl_pnlIfGeneral);
		
		lblIfCount = new JLabel("Iterface Count:");
		GridBagConstraints gbc_lblIfCount = new GridBagConstraints();
		gbc_lblIfCount.insets = new Insets(5, 5, 5, 5);
		gbc_lblIfCount.anchor = GridBagConstraints.EAST;
		gbc_lblIfCount.gridx = 0;
		gbc_lblIfCount.gridy = 0;
		pnlIfGeneral.add(lblIfCount, gbc_lblIfCount);
		
		txtIfCount = new JTextArea();
		txtIfCount.setEditable(false);
		GridBagConstraints gbc_txtIfCount = new GridBagConstraints();
		gbc_txtIfCount.insets = new Insets(5, 5, 5, 5);
		gbc_txtIfCount.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtIfCount.gridx = 1;
		gbc_txtIfCount.gridy = 0;
		pnlIfGeneral.add(txtIfCount, gbc_txtIfCount);
		
		lblIfInList = new JLabel("In List:");
		GridBagConstraints gbc_lblIfInList = new GridBagConstraints();
		gbc_lblIfInList.anchor = GridBagConstraints.EAST;
		gbc_lblIfInList.insets = new Insets(5, 5, 5, 5);
		gbc_lblIfInList.gridx = 2;
		gbc_lblIfInList.gridy = 0;
		pnlIfGeneral.add(lblIfInList, gbc_lblIfInList);
		
		txtIfInList = new JTextArea();
		txtIfInList.setText("0");
		txtIfInList.setEditable(false);
		GridBagConstraints gbc_txtIfInList = new GridBagConstraints();
		gbc_txtIfInList.insets = new Insets(5, 5, 5, 5);
		gbc_txtIfInList.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtIfInList.gridx = 3;
		gbc_txtIfInList.gridy = 0;
		pnlIfGeneral.add(txtIfInList, gbc_txtIfInList);
		
		lblIfActiveCount = new JLabel("Active Interfaces:");
		GridBagConstraints gbc_lblIfActiveCount = new GridBagConstraints();
		gbc_lblIfActiveCount.anchor = GridBagConstraints.EAST;
		gbc_lblIfActiveCount.insets = new Insets(5, 5, 5, 5);
		gbc_lblIfActiveCount.gridx = 0;
		gbc_lblIfActiveCount.gridy = 1;
		pnlIfGeneral.add(lblIfActiveCount, gbc_lblIfActiveCount);
		
		txtIfActive = new JTextArea();
		txtIfActive.setText("0");
		txtIfActive.setEditable(false);
		GridBagConstraints gbc_txtIfActive = new GridBagConstraints();
		gbc_txtIfActive.insets = new Insets(5, 5, 5, 5);
		gbc_txtIfActive.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtIfActive.gridx = 1;
		gbc_txtIfActive.gridy = 1;
		pnlIfGeneral.add(txtIfActive, gbc_txtIfActive);
		
		lblIfPerc = new JLabel("Active Percentage (%):");
		GridBagConstraints gbc_lblIfPerc = new GridBagConstraints();
		gbc_lblIfPerc.anchor = GridBagConstraints.EAST;
		gbc_lblIfPerc.insets = new Insets(5, 5, 5, 5);
		gbc_lblIfPerc.gridx = 2;
		gbc_lblIfPerc.gridy = 1;
		pnlIfGeneral.add(lblIfPerc, gbc_lblIfPerc);
		
		txtIfPerc = new JTextArea();
		txtIfPerc.setText("0");
		txtIfPerc.setEditable(false);
		GridBagConstraints gbc_txtIfPerc = new GridBagConstraints();
		gbc_txtIfPerc.insets = new Insets(5, 5, 5, 5);
		gbc_txtIfPerc.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtIfPerc.gridx = 3;
		gbc_txtIfPerc.gridy = 1;
		pnlIfGeneral.add(txtIfPerc, gbc_txtIfPerc);
		
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	}
	
	private void doConnect() {
		cmbSnmpVersion.setEnabled(false);
		cmbProtocol.setEnabled(false);
		txtAgentAddr.setEnabled(false);
		txtAgentPort.setEnabled(false);
		pnlMain.setVisible(true);
		btnSubmit.setText("Disconnect");
		
		String connectionString = cmbProtocol.getSelectedItem().toString() + ":" + txtAgentAddr.getText() + "/" + txtAgentPort.getText();
		int snmpVersion = -1;
		switch (cmbProtocol.getSelectedIndex()) {
		case 0:
			snmpVersion = SnmpConstants.version1;
		break;
		case 1:
			snmpVersion = SnmpConstants.version2c;
		break;
		case 2:
			snmpVersion = SnmpConstants.version3;
		break;
		}
		if (client != null) {
			try {
				client.stop();
			}
			catch (IOException e) { }
			client = null;
		}
		client = new SimpleSnmpClient(connectionString, snmpVersion);
		//fillGeneralInfo();
		//fillInterfaceInfo(null);
		
		GeneralInfo genInfo = new GeneralInfo(client, this);
		genInfo.start();
		
		InterfaceTableModel model = new InterfaceTableModel(client);
		model.addTableModelListener(this);
		table.setModel(model);
		
		table.getColumnModel().getColumn(0).setPreferredWidth(50);
		table.getColumnModel().getColumn(1).setPreferredWidth(150);
		table.getColumnModel().getColumn(2).setPreferredWidth(120);
		table.getColumnModel().getColumn(3).setPreferredWidth(120);
		table.getColumnModel().getColumn(4).setPreferredWidth(50);
		table.getColumnModel().getColumn(6).setPreferredWidth(80);
		table.getColumnModel().getColumn(7).setPreferredWidth(100);
		table.getColumnModel().getColumn(8).setPreferredWidth(100);
	}
	
	private void doDisconnect() {
		cmbSnmpVersion.setEnabled(true);
		cmbProtocol.setEnabled(true);
		txtAgentAddr.setEnabled(true);
		txtAgentPort.setEnabled(true);
		pnlMain.setVisible(false);
		btnSubmit.setText("Connect");
		
		if (pnlIfDet != null) {
			pnlIfDetails.remove(pnlIfDet);
			pnlIfDet.stop();
			pnlIfDet.setVisible(false);
			pnlIfDet = null;
		}
		
		try {
			if (client != null) {
				client.stop();
			}
		} catch (IOException e) { }
		
		client = null;
		
		txtIfActive.setText("");
		txtIfCount.setText("");
		txtIfInList.setText("");
		txtIfPerc.setText("");
		txtSysContact.setText("");
		txtSysDescr.setText("");
		txtSysLocation.setText("");
		txtSysName.setText("");
		txtSysUpTime.setText("");
		txtSegIn.setText("");
		txtErrsIn.setText("");
		txtTcpRetrans.setText("");
		txtAlg.setText("");
		txtTimeoutMax.setText("");
		txtTimeoutMin.setText("");
		txtEstNo.setText("");
		
		TableModel m = table.getModel();
		
		if ((m != null) && (m instanceof InterfaceTableModel)) {
			((InterfaceTableModel)m).clearData();
		}
	}
	
	/*private void fillGeneralInfo() {
		client.get(new OID[] {
			new OID(OidConsts.sysName + ".0"),
			new OID(OidConsts.sysDescr + ".0"),
			new OID(OidConsts.sysLocation + ".0"),
			new OID(OidConsts.sysContact + ".0"),
			new OID(OidConsts.sysUpTime + ".0"),
			new OID(OidConsts.ifNumber + ".0"),
			new OID(OidConsts.tcpInSegs + ".0"),
			new OID(OidConsts.tcpInErrs + ".0"),
			new OID(OidConsts.tcpRetransSegs + ".0"),
			new OID(OidConsts.tcpRtoAlgorithm + ".0"),
			new OID(OidConsts.tcpRtoMax + ".0"),
			new OID(OidConsts.tcpRtoMin + ".0"),
			new OID(OidConsts.tcpCurrEstab + ".0")
		}, this, 
		new Object[] {
				txtSysName,
				txtSysDescr,
				txtSysLocation,
				txtSysContact,
				txtSysUpTime,
				txtIfCount,
				txtSegIn,
				txtErrsIn,
				txtTcpRetrans,
				txtAlg,
				txtTimeoutMax,
				txtTimeoutMin,
				txtEstNo
		}, PDU.GET, null);
	}
	
	private void fillInterfaceInfo(VariableBinding[] bindings) {
		InterfaceInfo info = new InterfaceInfo();
		client.get(new OID[] {
				new OID(info.getNextOID())
			}, this, 
			new Object[] {
					info
			}, PDU.GETNEXT, bindings);
	}

	@Override
	public void onResponse(final ResponseEvent response) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				if (response == null) {
					System.out.println("Error while getting snmp response object");
				}
				else if (response.getError() != null) {
					System.out.println(response.getError().getMessage());
				}
				else if ((response.getUserObject() != null) && (response.getUserObject() instanceof Object[])) {
					Object[] userObjects = (Object[])response.getUserObject();
					for (int i = 0; i < userObjects.length; i++) {
						if (userObjects[i] != null) {
							if (userObjects[i] instanceof JTextArea) {
								JTextArea txtArea = (JTextArea)userObjects[i];
								//System.out.println("Setting value for object: " + txtArea.getName());
								String result = "";
								//ResponseEvent mResponse = response;
								if (response.getResponse() != null) {
									result = response.getResponse().get(i).getVariable().toString();
								
									double perc = 0.0;
									boolean isWorth = false;
									
									if (txtArea == txtErrsIn) {
										double sum = Double.parseDouble(txtSegIn.getText());
										double errs = (double)response.getResponse().get(i).getVariable().toInt();
										perc = (errs / sum) * 100;
										isWorth = true;
									}
									else if (txtArea == txtTcpRetrans) {
										double sum = Double.parseDouble(txtSegIn.getText());
										double retrans = (double)response.getResponse().get(i).getVariable().toInt();
										perc = (retrans / sum) * 100;
										isWorth = true;
									}
									else if ((txtArea == txtTimeoutMax) || (txtArea == txtTimeoutMin)) {
										isWorth = true;
										double val = (double)response.getResponse().get(i).getVariable().toInt() / (double)1000;
										DecimalFormat df = new DecimalFormat("#.##");
										result = df.format(val);
									}
									
									if (isWorth) {
										DecimalFormat df = new DecimalFormat("#.##");
										result = result + " (" + df.format(perc) + "%)";
									}
									
									if (result.length() == 0) {
										result = "<empty>";
									}
									txtArea.setText(result);
									
									
								}
							}
							else if (userObjects[i] instanceof InterfaceInfo) {
								InterfaceInfo info = (InterfaceInfo)userObjects[i];
								boolean valueSet = info.setCurrentValue(response.getResponse(), response.getRequest());
								if ((response.getResponse() != null) && (info.getBinding() != null)) {
									if (info.eof()) {
										InterfaceTableModel model = (InterfaceTableModel)table.getModel();
										model.addInterfaceInfo(info);
										fillInterfaceInfo(new VariableBinding[] { info.getBinding() } );
									}
									else {
										client.get(new OID[] {
												new OID(info.getNextOID() + "." + info.getId()),
											}, mform, 
											new Object[] {
												info
											}, PDU.GET, null);
									}
								}
							}
						} 
					}
				}
			}
			
		});
	}*/
		
	public static void main(String[] args) {
		new MainForm();
	}

	@Override
	public void tableChanged(TableModelEvent ev) {
		if (ev.getType() != TableModelEvent.INSERT) {
			return;
		}
		InterfaceTableModel model = (InterfaceTableModel)table.getModel();
		int countIf = model.getRowCount();
		
		txtIfInList.setText(countIf + "");
		
		int activeIf = model.getActiveInterfaces();
		txtIfActive.setText(activeIf + "");
		
		int realCountIf = 0;
		if (txtIfCount.getText().length() > 0) {
			realCountIf = Integer.parseInt(txtIfCount.getText());
		}
		
		double activeIfPerc = ((double)activeIf) / ((double)realCountIf) * 100;
		DecimalFormat df = new DecimalFormat("#.##");
		txtIfPerc.setText(df.format(activeIfPerc));
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting()) {
			return;
		}
		if (pnlIfDet != null) {
			pnlIfDetails.remove(pnlIfDet);
			pnlIfDet.stop();
			pnlIfDet.setVisible(false);
			pnlIfDet = null;
		}
		InterfaceTableModel model = (InterfaceTableModel)table.getModel();
		if (table.getSelectedRow() == -1) {
			return;
		}
		int selIndex = table.convertRowIndexToModel(table.getSelectedRow());
		int index = model.getIndexAtRow(selIndex);
		int speed = model.getSpeedAtRow(selIndex);
		
		pnlIfDet = new IfInfoDetailsPanel(client, index, speed);
		pnlIfDet.setVisible(true);
		pnlIfDetails.add(pnlIfDet, BorderLayout.CENTER);
		
		splitPane.validate();
	}
}
